<?php
	
	$connection = mysql_connect('mysql16.000webhost.com', 'a1863493_teamven', 'teamventure1') or die ("<p>Failed to Connect to Database</p>");
	$database = "a1863493_venture";
	mysql_select_db($database, $connection) or die ("<p>Failed to connect to database2</p>");
	
	$function = $_GET['function'];
	if($function == "login" and ISSET($_GET['username']) and ISSET($_GET['password'])) {
		login($_GET['username'],$_GET['password']);
	} else if($function == 'listMessages' and ISSET($_GET['customer_id'])) {
		listMessages($_GET['customer_id']);
	} else if($function == 'getCustID' and ISSET($_GET['username']) and ISSET($_GET['password'])) {
		getCustID($_GET['username'],$_GET['password']);
	}else if ($function == 'test') {
		$query = "SELECT idcust_acct FROM cust_acct WHERE name=\"test\" AND pass=\"pass\"";
		$result = mysql_query($query);
		if($result != null) {
			$rows = array();
			while($r = mysql_fetch_assoc($result)) {
				$rows[] = $r;
			}
			$json = json_encode($rows);
			echo "$json";
		} else {
			echo mysql_error();
			echo 'Didn\'t work...';
		}
	}
	
	/*
	$query = mysql_query("SELECT * FROM message WHERE message.idbus_acct = \"" . $fromCompany . "\")";
	$messages = array();
	while($row = mysql_fetch_assoc($query)) {
		$newMessage = new Message($row['message'], $row['idbus_acct']);
		array_pust($messages, $newMessage);
	}
	*/
	
	mysql_close($connection);
	
	function login($username="",$password="") {
		$query = "SELECT name,pass FROM cust_acct WHERE name=\"" . $username . "\" and pass=\"" . $password . "\"";
		$result = mysql_query($query);
		$rows = array();
		while($r = mysql_fetch_assoc($result)) {
			$rows[] = $r;
		}
		$json = json_encode($rows);
		echo "$json";
	}
	
	function listMessages($customer_id=null) {
		$query = "SELECT message.message, bus_acct.name FROM message, bus_acct, (SELECT idbus_acct FROM bus_cust WHERE idcust_acct = \"$customer_id\") AS x WHERE message.idbus_acct = x.idbus_acct AND message.idbus_acct = bus_acct.idbus_acct";
		$result = mysql_query($query);
		if($result != null) {
			$rows = array();
			while($r = mysql_fetch_assoc($result)) {
				$rows[] = $r;
			}
			$json = json_encode($rows);
			echo "$json";
		}
	}
	
	function getCustID($username="",$password="") {
		$query = "SELECT idcust_acct FROM cust_acct WHERE name=\"" . $username . "\" AND pass=\"" . $password . "\"";
		$result = mysql_query($query);
		if($result != null) {
			$rows = array();
			while($r = mysql_fetch_assoc($result)) {
				$rows[] = $r;
			}
			$json = json_encode($rows);
			echo "$json";
		}
	}
?>